package com.sms.biz.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.sms.biz.IReportBiz;
import com.sms.dao.IReportDAO;
import com.sms.dto.TestNumberDto;
import com.sms.po.TInvalidmsg;
import com.sms.po.TMsgRpt;
import com.sms.po.TSendmsg;
import com.sms.po.TWaitsend;
import com.sms.po.UserInfoSms;


/**
 * @author 20111009 by meng.xf
 */
public class ReportBizImpl implements IReportBiz {
	private IReportDAO reportDao = null;

	public void setReportDao(IReportDAO reportDao) {
		this.reportDao = reportDao;
	}

	@Override
	public List<TSendmsg> showSmsDetailByPage(String phone,int status,UserInfoSms curUser,int start,int limit) {
		// 离线查询
		DetachedCriteria criteria = DetachedCriteria.forClass(TSendmsg.class);
		if(curUser.getUserType()==2){//代理用户,可查询下级用户
			criteria.add(Restrictions.sqlRestriction("(user_id='"+curUser.getUserId()+"' or parent_id='"+curUser.getParentId()+"')"));
		}else if(curUser.getUserType()==3){//普通用户
			criteria.add(Restrictions.eq("userId", curUser.getUserId()));
		}
		
		if (!"".equals(phone) && null != phone) {
			criteria.add(Restrictions.eq("phone", phone));
		}
		if (start <= 0) {start = 0;		}
		if (limit <= 0) {limit = 10;	}
		return reportDao.showSmsDetailByPage(criteria, start, limit);
	}
	
	@Override
	public int getSmsDetailCount(String phone,int status,UserInfoSms curUser) {
		// 离线查询
		DetachedCriteria criteria = DetachedCriteria
				.forClass(TSendmsg.class);
		if(curUser.getUserType()==2){//代理用户,可查询下级用户
			criteria.add(Restrictions.sqlRestriction("(user_id='"+curUser.getUserId()+"' or parent_id='"+curUser.getParentId()+"')"));
		}else if(curUser.getUserType()==3){//普通用户
			criteria.add(Restrictions.eq("userId", curUser.getUserId()));
		}
		
		if (!"".equals(phone) && null != phone) {
			criteria.add(Restrictions.eq("phone", phone));
		}
		return reportDao.getSmsDetailCount(criteria);
	}

	@Override
	public int getTestNumberCount(String userId) {
		return reportDao.getTestNumberCount(userId);
	}

	@Override
	public List<TestNumberDto> showTestNumberByPage(String userId, int start, int limit) {
		if (start <= 0) {start = 0;		}
		if (limit <= 0) {limit = 10;	}
		return reportDao.showTestNumberByPage(userId, start, limit);
	}
	
	@Override
	public int getTestNumber2Count(String userId,String searchStr) {
		userId=userId.replaceAll("'", "");
		searchStr=searchStr.replaceAll("'", "");
		searchStr=searchStr.replaceAll("%", "");
		
		return reportDao.getTestNumber2Count(userId,searchStr);
	}

	@Override
	public List<TestNumberDto> showTestNumber2ByPage(String userId, String searchStr, int start, int limit) {
		userId=userId.replaceAll("'", "");
		searchStr=searchStr.replaceAll("'", "");
		searchStr=searchStr.replaceAll("%", "");
		if (start <= 0) {start = 0;		}
		if (limit <= 0) {limit = 10;	}
		return reportDao.showTestNumber2ByPage(userId, searchStr,start, limit);
	}
	

	@Override
	public int getRouteSendReportCount(Map<String, String> map) {
		return reportDao.getRouteSendReportCount(map);
	}

	@Override
	public int getUserSendReportCount(Map<String, String> map) {
		return reportDao.getUserSendReportCount(map);
	}

	@Override
	public List<TMsgRpt> showRouteSendReportByPage(Map<String, String> map, int start, int limit) {
		return reportDao.showRouteSendReportByPage(map, start, limit);
	}

	@Override
	public List<TMsgRpt> showUserSendReportByPage(Map<String, String> map, int start, int limit) {
		return reportDao.showUserSendReportByPage(map, start, limit);
	}
	
	@Override
	public int getInvalidmsgCount(String userId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TInvalidmsg.class);
		// 如果条件标题不为空,那么将条件标题附加上
		if (!"".equals(userId) && null != userId) {
			criteria.add(Restrictions.eq("userId", userId));
		}
		return reportDao.getInvalidmsgCount(criteria);
	}

	@Override
	public List<TInvalidmsg> showInvalidmsgByPage(String userId, int start, int limit) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TInvalidmsg.class);
		// 如果条件标题不为空,那么将条件标题附加上
		if (!"".equals(userId) && null != userId) {
			criteria.add(Restrictions.eq("userId", userId));
		}
		criteria.addOrder(Order.desc("id"));
		if (start <= 0) {start = 0;		}
		if (limit <= 0) {limit = 10;	}
		return reportDao.showInvalidmsgByPage(criteria, start, limit);
	}

	@Override
	public int getWaitsendSmsCount(UserInfoSms curUser,int status) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TWaitsend.class);
		//0-待发送，1-已发送 <==> t_waitsend表字段：-1-待审核,0-待发送,1-已发送,2-发送成功,3-发送失败,4-审核不通过
		if(status==0){
			criteria.add(Restrictions.sqlRestriction("(status='-1' or status='0')"));
		}else if(status==1){
			criteria.add(Restrictions.sqlRestriction("(status='1' or status='2' or status='3')"));
		}
		
		// 如果条件标题不为空,那么将条件标题附加上
		//if (!"".equals(userId) && null != userId) {
			criteria.add(Restrictions.eq("userId", curUser.getUserId()));
		//}
		return reportDao.getWaitsendSmsCount(criteria);
	}
	
	@Override
	public List<TWaitsend> showWaitsendSmsByPage(UserInfoSms curUser, int status, int start, int limit) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TWaitsend.class);
		//0-待发送，1-已发送
		//0-待发送，1-已发送 <==> t_waitsend表字段：-1-待审核,0-待发送,1-已发送,2-发送成功,3-发送失败,4-审核不通过
		if(status==0){
			criteria.add(Restrictions.sqlRestriction("(status='-1' or status='0')"));
		}else if(status==1){
			criteria.add(Restrictions.sqlRestriction("(status='1' or status='2' or status='3')"));
		}
		
		// 如果条件标题不为空,那么将条件标题附加上
		//if (!"".equals(userId) && null != userId) {
			criteria.add(Restrictions.eq("userId", curUser.getUserId()));
		//}
		criteria.addOrder(Order.desc("id"));
		if (start <= 0) {start = 0;		}
		if (limit <= 0) {limit = 10;	}
		return reportDao.showWaitsendSmsByPage(criteria, start, limit);
	}

	
	
}
